
var compose = require('compose-middleware').compose;//中间件打包
var unzip = require('../src/unzip.js');
var path = require('path');
var upload = require('../src/upload.js');
var we7pack = require('../src/we7pack.js');
// 解压
function unzipmiddle(req, res, next) {
	if (!req.file) {
		throw 'no file';
	}
	var file = req.file;
	var filename = file.filename;
	unzip(filename).then(function(){
		next();
	});
}

// 打包成一个文件
function pack(req, res, next) {
	var file = req.file;
	var filename = file.filename;
	// filename = '06f4b280-944a-11e7-8917-a173b37335f8';
	var basename = path.basename(filename,'.zip');
	var src = path.resolve(__dirname,'../storage/unzip/'+basename);
	var dest = path.resolve(__dirname,'../storage/wx/'+basename+'.wx');
	we7pack(src, dest).then(()=>{
		next();
	},(error)=>{throw error;});
}

function test() {
	var filename = '909af730-9457-11e7-8d99-b178c03bc06e.zip';
	unzip(filename).then(function(){
		var basename = path.basename(filename,'.zip');
		var src = path.resolve(__dirname,'../storage/unzip/'+basename);
		var dest = path.resolve(__dirname,'../storage/wx/'+basename+'.wx');
		console.log('开始打包');  
		we7pack(src, dest).then(()=>{
			console.log('打包完成');
		},(error)=>{throw error;});
		
	});
}
// test();
module.exports = compose(upload.single('wxpack'), unzipmiddle, pack);